new Como.Pinboard({
	element: Como('div.pic_list').get(0),
	width: 222,
	padding: 12,
	center: false
}).arrange();

Como.onloadHandler(function(){
	var ll = new Como.Lazyload();
	ll.load();
});

var ilike={
	haoturn_url:'http://huangximing.woxihuan.com/win/haoturn?jsoncallback=?',
	turning:false,
	submit_text:'提交中...',
	post_num:0,
	guid:(function() {
	    var S4 = function() {
	       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	    };
	    return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
	})(),
	hd_tab_init:function(){
		new Como.Tab({
			tabs: 'ul.cat-tag li a',
			panels: 'div.like_content',
			activeClass: 'selected',
			activeEvent: 'click',
			defaultActive: 0,
			onActive: function(key, index, _this){
				new Como.Pinboard({
					element: Como('div.pic_list').get(index),
					width: 222,
					padding: 12,
					center: false
				}).arrange();
			}
		});
	},
	hd_tab_anim:function(){
		setInterval(function(){
			var obj = $("#ay_hot");
			obj.first().anim().to("marginTop","-22px").ondone(function(){
				var old = obj.first();
				obj.first().remove();
		        	obj.append(old.css("marginTop","0px"));
		        }).go();
	    },5000);
	},
	init:function(){
		$("#ilike .pic_list").delegate(".item","mouseenter",function(){
			$(this).find(".tool-bar").show();
		}).delegate(".item","mouseleave",function(){
			$(this).find(".tool-bar").hide();
		});
		$("#ilike").delegate(".collect-btn","click",function(){
			ilike.post_colection($(this));
			
		});
	},
	post_colection:function(_this){
		if(!ilike.turning){
			ilike.turning=true;
			var itemObj=_this.parents(".item:first"),
				data_key=itemObj.attr("data-uid") + '_' + itemObj.attr("data-bid") + '_',
				data = {
					'key':data_key + itemObj.attr("data-albid"),
					'type' : itemObj.attr("data-type"),
					'view_type' : itemObj.attr("data-view_type"),
					'title':itemObj.attr("data-title"),
					'popu_form':0,
					'guid':ilike.guid,
					'rand' : + new Date()
				},
				formObj=$("#popu_form")
			;
			if(formObj[0]){
				data.popu_form=1;
				data.key=data_key + $("#my_albums option:selected").val();
				data.title=$.trim($("#my_title").val());
				if(!!$("#my_type").attr("checked")){
					data.type=1;
				}
				if(data.title==""){
					data.title=itemObj.attr("data-title");
				}
				if(data.title==""){
					ilike.set_save_btn();
					$("#my_title").focus();
					return false;
				}
			}
			$.getJSON(ilike.haoturn_url,data,function(data){
				var title="收集到我喜欢",height=290,dt;
				if(data.repost==1){
					ilike.turning=false;
					ilike.post_num=ilike.post_num+1;
					if(ilike.post_num<10){
						ilike.post_colection(_this);
					}
					
					return false;
				}
				if(data.my_qid>0){
					if(data.popu_form==1){
						if(data.login_no_albums!=1){
							data.html='<div id="popu_form">'+data.html+'</div>';
						}else{
							height=120;
						}
					}else{
						height=120;
					}
				}
				var d=$.dialog({
						title:title,
						content:(data.popu_form==1?data.html:'<div class="turnlist-dialog-bd"'+(data.my_qid>0?' style="background-image:none"':'')+'>'+data.html+'</div>'),
						padding:0,
						animstart:false,
						width:608,
						height:height,
						beforeclose :function(){
							ilike.turning=false;
						}
				}).show();
				
				if($("#popu_form")[0]){
					$("#my_title").Limitchar({ txtNote: "#my_title", is_submit: true, form_obj:_this, limitCount:140 });
				}
				if(data.my_qid>0&&data.popu_form!=1&&data.login_no_albums!=1){
					clearTimeout(dt);
					dt=setTimeout(function(){
						d.close();
					},3000);
				}
				ilike.turning=false;
				if(data.login_no_albums==1){
					ilike.post_num=ilike.post_num+1;
					if(ilike.post_num<10){
						$("#login_iframe").load(function(){
							ilike.post_colection(_this);
						});
					}
				}else{
					ilike.post_num=0;
				}
			});
		}
	},
	form_hint_time:null,
	show_form_hint:function(text){
		if(!$("#popu_form_hint")[0]){
			$("#popu_form").append('<div id="popu_form_hint" style="left: 130px; top: 192px; position: absolute;height: 28px;padding: 0 6px;font-size: 14px;line-height: 28px;border: 1px solid #F89878;border-radius: 5px;color: #CB2027;background: #FFDCD2;z-index: 1000;display:none;"></div>');
		}
		clearTimeout(ilike.form_hint_time);
		$("#popu_form_hint").text(text).fadeIn(300,function(){
			ilike.form_hint_time=setTimeout(function(){
				$("#popu_form_hint").fadeOut(300);
			},1000);
		});
	},
	set_save_btn:function(type){
		var html='<span style="color:#ccc">'+ilike.submit_text+'</span>';
		if(type!="saving"){
			html='完成';
		}
		$("#save_collection").html(html);
	},
	show_dialog:function(){
		
	}
};
//Limitchar $("#txtNote").Limitchar({ txtNote: "#txtNote", txtLimit: "#txtLimit", limitCount:140 });
(function($) {
    $.fn.Limitchar = function(options) {
        var defaults = {
            txtNote: '',
            txtLimit: '',
            limitCount: 20,
            isbyte: false,
            is_submit:false,
            form_obj:null
        };
        var opts = $.extend(defaults, options);
        if(!(opts.is_submit)){
	        $(this).keyup(function() {
	            wordsLimit()
	        });
	        $(this).keydown(function() {
	            wordsLimit()
	        });
	        $(this).keydown(function() {
	            wordsLimit()
	        });
	        this.each(function() {});
	        wordsLimit();
        }else{
        	$("#save_collection").click(function(){
        		if($(this).text()!=ilike.submit_text){
        			if(wordsLimit()){
						ilike.set_save_btn("saving");
						ilike.post_colection(opts.form_obj);
					}
				}
        	});
        }
        function wordsLimit() {
            var noteCount = 0;
            if (opts.isbyte) {
                noteCount = $(opts.txtNote).val().replace("/[^/x00-/xff]/g", "xx").length
            } else {
                var d = ($(opts.txtNote).val().match(/[\x00-\xff]/g) || " ").length;
                noteCount = Math.ceil($(opts.txtNote).val().length - d / 2)
            }
            if (noteCount > opts.limitCount) {
            	if(!(opts.is_submit)){
	                if (opts.isbyte) {
	                    $(opts.txtNote).val($(opts.txtNote).val().substring(0, opts.txtlength + Math.floor((opts.limitCount - opts.txtByte) / 2)));
	                    $(opts.txtLimit).text(opts.limitCount - opts.txtByte)
	                } else {
	                    $(opts.txtNote).val($(opts.txtNote).val().substring(0, opts.limitCount));
	                    $(opts.txtLimit).text(0)
	                }
	            }else{
	            	ilike.show_form_hint("标题不能超过140个汉字");
	            	return false;
	            }
            } else {
            	if(!(opts.is_submit)){
	                $(opts.txtLimit).text(opts.limitCount - noteCount);
	            }else{
	            	return true;
	            }
            }
        }
    }
})(jQuery);